﻿<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<c:set var="basePath" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>新增角色</title>
    <jsp:include page="/resources/inc/formHead.jsp"/>
</head>
<body>
<div id="createForm" class="layui-form layui-form-pane" method="post">
    <div class="layui-form-item">
        <ul id="ztree" class="ztree"></ul>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="permissionSubmit();">立即提交</button>
        </div>
    </div>
</div>
<jsp:include page="/resources/inc/formFooter.jsp"/>
<script>
    $(function () {
        initTree();
    })
    var selectSystems = [];
    var selectPermission = [];
    var setting = {
        check: {
            enable: true,
            // 勾选关联父，取消关联子
            chkboxType: {"Y": "ps", "N": "s"}
        },
        async: {
            enable: true,
            url: '${basePath}/manage/permission/role/${role.roleId}'
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onCheck: function () {
                var zTree = $.fn.zTree.getZTreeObj("ztree")
                var changeNodes = zTree.getChangeCheckedNodes();
                selectPermission = [];
                selectSystems = [];
                $(changeNodes).each(function (index, node) {
                    if (node.isSystem) {
                        selectSystems.push({id: node.realId, checked: node.checked});
                    } else {
                        selectPermission.push({id: node.realId, checked: node.checked});
                    }
                });
            }
        }
    };
    function initTree() {
        $.fn.zTree.init($('#ztree'), setting);
    }

    function permissionSubmit() {
        $.ajax({
            type: 'post',
            url: '${basePath}/manage/role/permission/' + ${role.roleId},
            data: {permissions: JSON.stringify(selectPermission),systems: JSON.stringify(selectSystems)},
            success: function (result) {
                if (result.code != 1) {
                    if (result.data instanceof Array) {
                        $.each(result.data, function (index, value) {
                            layer.msg(value.errorMsg);
                        });
                    } else {
                        layer.msg(result.data.errorMsg);
                    }
                } else {
                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                    parent.layer.close(index); //再执行关闭
                    parent.$table.bootstrapTable('refresh');
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.msg(textStatus);
            }
        });
    }
</script>
</body>
</html>

